package com.wangtao.firstapp.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.modles.MessageBoardData;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 留言板适配器
 * Created by lanouhn on 16/9/10.
 */
public class MessageBoardAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private MessageBoardData messageBoardData;
    private List contents;
    private List datestrs;

    public MessageBoardAdapter(Context context, MessageBoardData messageBoardData) {
        this.context = context;
        this.messageBoardData = messageBoardData;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case 0:
                View view_mine = LayoutInflater.from(context).inflate(R.layout.item_mine_recyclerview_messageboard, null);
                holder = new MineViewHolder(view_mine);
                break;
            case 1:
                View view_offical = LayoutInflater.from(context).inflate(R.layout.item_offical_recyclerview_messageboard, null);
                holder = new OfficalViewHolder(view_offical);
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
//        for (int i = 0; i < messageBoardData.getData().getList().size(); i++) {
//            contents = new ArrayList<>();
//            datestrs = new ArrayList<>();
//            contents.add(messageBoardData.getData().getList().get(i).getContent());
//            datestrs.add(messageBoardData.getData().getList().get(i).getDatestr());
//        }
//        Collections.reverse(contents);
//        Collections.reverse(datestrs);

        int type = messageBoardData.getData().getList().get(position).getUser().getIs_official();
        switch (type) {
            case 0:
                MineViewHolder mineViewHolder = (MineViewHolder) holder;
                Picasso.with(context).load(messageBoardData.getData().getList().get(position).getUser().getAvatar()).config(Bitmap.Config.RGB_565).into(mineViewHolder.cir_header_mine);
                mineViewHolder.tv_mine_message.setText(messageBoardData.getData().getList().get(position).getContent());
                mineViewHolder.tv_datestr_messageboard_mine.setText(messageBoardData.getData().getList().get(position).getDatestr());
                if (messageBoardData.getData().getList().get(position).getIs_default() == 1) {
                    mineViewHolder.tv_datestr_messageboard_mine.setVisibility(View.GONE);
                }
                break;
            case 1:
                OfficalViewHolder officalViewHolder = (OfficalViewHolder) holder;
                Picasso.with(context).load(messageBoardData.getData().getList().get(position).getUser().getAvatar()).config(Bitmap.Config.RGB_565).into(officalViewHolder.cir_header_offical);
                officalViewHolder.tv_offical_message.setText(messageBoardData.getData().getList().get(position).getContent());
                officalViewHolder.tv_datestr_messageboard_offical.setText(messageBoardData.getData().getList().get(position).getDatestr());
                if (messageBoardData.getData().getList().get(position).getIs_default() == 1) {
                    officalViewHolder.tv_datestr_messageboard_offical.setVisibility(View.GONE);
                }
                break;
        }
    }

    @Override
    public int getItemCount() {
        return messageBoardData.getData().getList().size();
    }

    @Override
    public int getItemViewType(int position) {
        return messageBoardData.getData().getList().get(position).getUser().getIs_official();
    }

    public class OfficalViewHolder extends RecyclerView.ViewHolder {
        // 小糖君发的信息
        private CircleImageView cir_header_offical;
        private TextView tv_offical_message;
        private TextView tv_datestr_messageboard_offical;

        public OfficalViewHolder(View itemView) {
            super(itemView);
            cir_header_offical = (CircleImageView) itemView.findViewById(R.id.cir_header_offical);
            tv_offical_message = (TextView) itemView.findViewById(R.id.tv_offical_message);
            tv_datestr_messageboard_offical = (TextView) itemView.findViewById(R.id.tv_datestr_messageboard_offical);
        }
    }

    public class MineViewHolder extends RecyclerView.ViewHolder {
        // 我发的信息
        private CircleImageView cir_header_mine;
        private TextView tv_mine_message;
        private TextView tv_datestr_messageboard_mine;

        public MineViewHolder(View itemView) {
            super(itemView);
            cir_header_mine = (CircleImageView) itemView.findViewById(R.id.cir_header_mine);
            tv_mine_message = (TextView) itemView.findViewById(R.id.tv_mine_message);
            tv_datestr_messageboard_mine = (TextView) itemView.findViewById(R.id.tv_datestr_messageboard_mine);
        }
    }
}
